/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "LOGR_create_webgraph.h"

PNODE
LOGR_create_new_node_in_graph(WebStatPtr wsf,GraphFrame *gf,GraphFrame *ngf,
			      char *url,char *tit, char *smime,int sizeb,
			      int nacc);

void
LOGR_create_webgraph_from_user_graphs(WebStatPtr wsf,GraphFrame *gf,
				      GraphFrame *ngf)
{
  LNode_type *lgraphs = NULL;
  LNode_type *ptr = NULL;
  short cus = LOGR_get_compute_user_sessions(wsf);

  if(!cus)
      return;

  if(!wsf || !ngf)
      return;

  lgraphs = LOGR_get_list_graphs(wsf);
  
  for(ptr = lgraphs->back; ptr!=lgraphs; ptr=ptr->back)
  {
      WebUgraphPtr wup = (WebUgraphPtr)ptr->info;
      void *GArray = wup ? LOGR_get_ugraph_info(wup) : NULL;
      int nedges =  GArray ? LOGR_get_number_edges(GArray) : NULL;
      int ie = 0;
      char *urls = NULL, *urlt = NULL;
      char *tit = NULL;
      char *smime = NULL;
      int sizeb = 0;
      int numacc = 0;
      WebGenLinkPtr gep = NULL;
      PNODE v = NULL;
      PNODE u = NULL;
      PEDGE ne = NULL;
      long lacc = 0;
      char sacc[20];

      for(ie = 0; ie < nedges ; ie++)
      {
	  gep = LOGR_get_gen_edge_i(GArray,ie);
	  if(!gep)
	      continue;
	  lacc = LOGR_get_gen_link_naccesses(gep);
	  sprintf(sacc,"%ld",lacc);
	  urls = (char *)LOGR_get_gen_url_source(gep);	
	  tit = (char *)LOGR_get_gen_title_source(gep);
	  smime = (char *)LOGR_get_gen_mime_source(gep);
	  sizeb = LOGR_get_gen_size_source(gep);
	  numacc = LOGR_get_gen_naccesses_source(gep);
	  u = find_vertex_by_name(ngf,urls);
	  if(!u)
	  {
	    u = LOGR_create_new_node_in_graph(wsf,gf,ngf,urls,tit,smime,
					      sizeb,numacc);
	  }

	  urlt = (char *)LOGR_get_gen_url_target(gep);
	  tit = (char *)LOGR_get_gen_title_target(gep);
	  smime = (char *)LOGR_get_gen_mime_target(gep);
	  sizeb = LOGR_get_gen_size_target(gep);
	  numacc = LOGR_get_gen_naccesses_source(gep);
	  v = find_vertex_by_name(ngf,urlt);
	  if(!v)
	  {
	    v= LOGR_create_new_node_in_graph(wsf,gf,ngf,urlt,tit,smime,
					     sizeb,numacc);
	  }
	  ne = find_edge(ngf,u,v);
	  if(!ne && u != v)
	  {
	    adding_edge(ngf,u,v,sacc);
	  }
      }
  }
}

PNODE
LOGR_create_new_node_in_graph(WebStatPtr wsf,GraphFrame *gf,GraphFrame *ngf,
			      char *url,char *tit, char *smime,int sizeb,
			      int nacc)
{
  PNODE u = NULL;
  char sw[100];

  if(!wsf || !gf || !ngf || !url)
    return NULL;
  sprintf(sw,"%d",sizeb);

  /*printf("Adding Vertex %s\n",url);*/
  u = create_node_in_graph_name(ngf, url);
  change_weight_vertex(ngf, u,sw);
  add_attribute(u, "size",sw);
  if(tit)
      add_attribute(u, "title",tit);
  if(smime)
      add_attribute(u, "mime",smime);
  set_vertex_counter(u,nacc);
  return u;
}

void
LOGR_create_webgraph_from_logs_links(WebStatPtr wsf,GraphFrame *gf,
				       GraphFrame *ngf)
{
  LNode_type *lilinks = LOGR_get_list_independent_links(wsf);
  LNode_type *lnodes = get_list_vertices(gf);
  LNode_type *ptr = NULL;

  if(!wsf || !gf || !ngf)
      return;

  /* Independent Links */
  for(ptr = lilinks->back; ptr!=lilinks; ptr=ptr->back)
  {
    WebIndependentLinkPtr wil = (WebIndependentLinkPtr)ptr->info;
    char *urls = NULL, *urlt = NULL;
    char *tit = NULL;
    char *smime = NULL;
    int sizeb = 0;
    int numacc = 0;
    PNODE v = NULL;
    PNODE u = NULL;
    PEDGE ne = NULL;
    long lacc = 0;
    char sacc[20];

    lacc = LOGR_get_link_naccesses(wil);
    sprintf(sacc,"%ld",lacc);

    urls = (char *)LOGR_get_link_source_url(wil);	
    tit = (char *)LOGR_get_link_source_title(wil);
    smime = (char *)LOGR_get_link_source_mime(wil);
    sizeb = LOGR_get_link_source_size(wil);
    numacc = LOGR_get_link_source_naccesses(wil);
    u = find_vertex_by_name(ngf,urls);
    if(!u)
    {
      u = LOGR_create_new_node_in_graph(wsf,gf,ngf,urls,tit,smime,
					sizeb,numacc);
    }
    urlt = (char *)LOGR_get_link_target_url(wil);	
    tit = (char *)LOGR_get_link_target_title(wil);
    smime = (char *)LOGR_get_link_target_mime(wil);
    sizeb = LOGR_get_link_target_size(wil);
    numacc = LOGR_get_link_target_naccesses(wil);
    v = find_vertex_by_name(ngf,urlt);
    if(!v)
    {
      v= LOGR_create_new_node_in_graph(wsf,gf,ngf,urlt,tit,smime,
				       sizeb,numacc);
    }
    ne = find_edge(ngf,u,v);
    if(!ne && u != v)
    {
	/*printf("ADDING INDEPENDENT EDGE %s-%s\n",urls,urlt);*/
      adding_edge(ngf,u,v,sacc);
    }
 }

  for(ptr = lnodes->back; ptr != lnodes; ptr=ptr->back)
    {
      PNODE u = (PNODE)ptr->info;
      PNODE v = NULL;
      PNODE nu = NULL;
      PNODE nv = NULL;
      char *urls = NULL, *urlt = NULL;
      LNode_type *peo, *ledges_out = get_edges_out(u);
      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  PEDGE ne = NULL;
	  char *we = get_weight_edge(e);
	  int cedge = we ? atoi(we) : 0;
	  v = get_to_vertex(e);	
	  if(cedge > 0)
	  {
	    urls = get_label_vertex(u);
	    urlt = get_label_vertex(v);
	    nu = find_vertex_by_name(ngf,urls);
	    if(!nu)
	    {
		/*nu = copy_complete_vertex(ngf,u);*/
		nu = create_node_in_graph_name(ngf, urls);
		copy_vertex_info(nu,u);
	    }
	    nv = find_vertex_by_name(ngf,urlt);
	    if(!nv)
	    {
		/*nv = copy_complete_vertex(ngf,v);*/
		nv = create_node_in_graph_name(ngf, urlt);
		copy_vertex_info(nv,v);
	    }
	    ne = find_edge(ngf,nu,nv);
	    if(!ne && nu != nv)
	    {
		/*adding_edge(ngf,nu,nv,sacc);*/
		/*printf("ADDING GRAPH EDGE %s-%s\n",urls,urlt);*/
		ne = copy_complete_edge(ngf,e,nu,nv);
	    }    
	    
	  }
	}
    }

}


